/**
 * PHOSIDE: PHosphorylation Site IDentification Engine.
 * Copyright 2009 Digital Biology Lab, University of Missouri.
 * This library is free software; you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation; either version 3 of the License, or (at your option) any later
 * version. <p/> This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the License for more
 * details.
 */

package phoside.disorder;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;

import java.util.List;
import java.util.Map;
import java.util.Set;

import phoside.util.StaticFinalValues;

/**
 *
 * @author gjj
 */
public class DisorderInfoBatchWriterFasta implements DisorderInfoBatchWriter {

    protected final String dirFasta;

    private final int bufSize = 10000;

    public DisorderInfoBatchWriterFasta(final String dirFasta) {
        if (dirFasta==null) {
            throw new NullPointerException();
        }

        this.dirFasta = dirFasta;
    }

    public void write(final Map<String,List<Double>> mapAccDisorder) throws IOException {
        if (mapAccDisorder==null) {
            throw new NullPointerException();
        }

        Writer fout = new FileWriter(dirFasta);
        BufferedWriter out = new BufferedWriter(fout);

        StringBuilder strscores = new StringBuilder(bufSize);

        Set<Map.Entry<String,List<Double>>> entrySet = mapAccDisorder.entrySet();
        for (Map.Entry<String,List<Double>> entry : entrySet) {
            out.write(">"+entry.getKey());
            out.newLine();

            strscores.setLength(0);

            List<Double> scores = entry.getValue();
            int n = scores.size();
            
            for (int i=0; i<n; i++) {
                double score = scores.get(i);
                strscores.append(score);
                strscores.append(StaticFinalValues.SEPARATOR);
            }
            
            int len = strscores.length();
            strscores.delete(len-StaticFinalValues.SEPARATOR.length(), len);

            out.write(strscores.toString());
            out.newLine();
        }

        out.close();
        fout.close();
    }
}
